﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Story.Framework;
using Story.Framework.WinUI;
using Storys.Collect;
using Storys.Collect.Entity;

namespace Storys.Collect.WinUI
{
    public partial class NewSitePage : PageBase
    {
        SitesManager manSites = new SitesManager();
        private int m_Id = -1;
        public NewSitePage()
        {
            InitializeComponent();
        }
        public NewSitePage(int id)
            : this()
        {
            this.m_Id = id;
        }
        public override void UpdateData()
        {
            if (CheckData())
            {
                SaveData();
            }
            base.UpdateData();
        }

        public override void RefreshData()
        {
            dsSite.SitesTable.Clear();
            dsSite.SitesTable.Merge(manSites.LoadSites());
            base.RefreshData();
        }

        public override void SendMessageDo(string key, string msg)
        {
            if (key.Equals("NewSitePage"))
            {
                m_Id = Convert.ToInt32(msg);
                SetData();
            }
            base.SendMessageDo(key, msg);
        }

        private void SetData()
        {
            SiteDS.SitesTableRow row = dsSite.SitesTable.FindByfldID(m_Id);
            if (row == null)
            {
                teName.Text = string.Empty;
                teUrl.Text = string.Empty;
                MEDesc.Text = string.Empty;
            }
            else
            {
                teName.Text = row.fldName;
                teUrl.Text = row.fldUrl;
                MEDesc.Text = row.fldDesc;
            }
        }

        private void SaveData()
        {
            if (dsSite.SitesTable.FindByfldID(m_Id) == null)
            {
                SiteDS.SitesTableRow row = dsSite.SitesTable.NewSitesTableRow();
                row.fldDesc = MEDesc.Text;
                row.fldName = teName.Text;
                row.fldUrl = teUrl.Text;
                row.fldWriteDate = AppClient.MainForm.ServerTime;
                dsSite.SitesTable.AddSitesTableRow(row);
                manSites.UpdateSite(dsSite.SitesTable);
            }
            else
            {
                SiteDS.SitesTableRow row = dsSite.SitesTable.FindByfldID(m_Id);
                row.fldDesc = MEDesc.Text;
                row.fldName = teName.Text;
                row.fldUrl = teUrl.Text;
                row.fldWriteDate = AppClient.MainForm.ServerTime;
                manSites.UpdateSite(dsSite.SitesTable);
            }
            AppClient.SendMessageToDock("Sites", "Refresh");
        }

        private bool CheckData()
        {
            if (string.IsNullOrEmpty(teName.Text))
            {
                AppClient.MainForm.ShowMsg("【网站名称不能为空】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                teName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(teUrl.Text))
            {
                AppClient.MainForm.ShowMsg("【网站地址不能为空】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                teUrl.Focus();
                return false;
            }
            return true;
        }

        private void sbNewSite_Click(object sender, EventArgs e)
        {
            m_Id = -1;
            SetData();
        }
    }
}
